﻿using System;
using System.IO;
using System.Collections;
using System.Xml.Serialization;
using ClassFactory;
using Engine;
using EventBroker;
using Algorithm;

namespace Configuration
{
    /// <summary>
    /// This class serialize and deserialize all the information of the websites to be crawled
    /// </summary>
    public class CAlgorithmSetup
    {
        private CSerializedWebPageGroupToJump m_serializedWebPages;
        private ArrayList m_aWebPageConcreteFactories;
               
        public ArrayList WebPageConcreteFactories
        {
            get
            {
                return m_aWebPageConcreteFactories;
            }
        }

        /// <summary>
        /// Constructor
        /// </summary>
        public CAlgorithmSetup (string strFileName_p)
		{
            m_aWebPageConcreteFactories = new ArrayList();
            //SerializeWebPages is to do testing
            SerializeWebPages(strFileName_p);
            DeserializeWebPages(strFileName_p);
            InitializeJumpingStyles();            
		}

        /// <summary>
        /// Deserialize the web pages information that is going to be parsed
        /// </summary>
        public void DeserializeWebPages(string strFileName_p)
        {
            FileStream fsFileStream = new FileStream(strFileName_p, FileMode.Open);
            XmlSerializer xsXmlSerializer = new XmlSerializer(typeof(CSerializedWebPageGroupToJump));
            m_serializedWebPages = (CSerializedWebPageGroupToJump)xsXmlSerializer.Deserialize(fsFileStream);
        }

        /// <summary>
        /// Testing : Serialize the web pages information that is going to be parsed to a xml file
        /// </summary>
        public void SerializeWebPages(string strFileName_p)
        {
            TextWriter textWriter = new StreamWriter(strFileName_p);
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(CSerializedWebPageGroupToJump));
            CSerializedWebPageGroupToJump serializedWebPagesToJump = new CSerializedWebPageGroupToJump();

            CSerializedWebPage serializedWebPage1 = new CSerializedWebPage();
            serializedWebPage1.Name = "Maybank";
            serializedWebPage1.NeedToJump = "true";
            serializedWebPage1.WebPageConcreteFactoryType = "Algorithm.CWebPageConcreteFactory_MayBank,WJ.Service";
            serializedWebPage1.RemoteLocation = "Local";

            CSerializedWebPage serializedWebPage2 = new CSerializedWebPage();
            serializedWebPage2.Name = "AffinBank";
            serializedWebPage2.NeedToJump = "true";
            serializedWebPage2.WebPageConcreteFactoryType = "Algorithm.CWebPageConcreteFactory_AffinBank,WJ.Service";
            serializedWebPage2.RemoteLocation = "Local";

            CSerializedWebPage[] aWebPagesToJump = new CSerializedWebPage[2] { serializedWebPage1, serializedWebPage2 };
            serializedWebPagesToJump.WebPages = aWebPagesToJump;
            xmlSerializer.Serialize(textWriter, serializedWebPagesToJump);
            textWriter.Close();
        }

        /// <summary>
        /// Create the WebPageConcreteFactory_AAA based on the deserialized information
        /// </summary>
        public void InitializeJumpingStyles()
        {
            foreach (CSerializedWebPage serializedWebPageToJump in m_serializedWebPages.WebPages)
            {
                if (Convert.ToBoolean(serializedWebPageToJump.NeedToJump) == true)
                {
                    CWebPageAbstractFactory WebPageConcreteFactory = (CWebPageAbstractFactory)ClassFactory.CClassFactory.GetFactory(serializedWebPageToJump);
                    CEventManager.Register(WebPageConcreteFactory.GetJumpingStyleConcreteProduct());
                    CEventManager.Register(WebPageConcreteFactory.GetParserConcreteProduct());
                    m_aWebPageConcreteFactories.Add(WebPageConcreteFactory);

                    
                }
            }            
        }      
    }
}
